const Router = require("@koa/router")();
const Util = require("../../util");
const { TestUser, TempType, TempList, TempComData } = require("../schema");

Router.post("/register", async (ctx) => {
  const { username, password } = ctx.request.body;
  const result = await TestUser.findOne({ username, password });
  if (result) return (ctx.body = Util.back(-400));
  return (ctx.body = await new TestUser({ username, password })
    .save()
    .then((res) => {
      return {
        data: res.username,
        code: 0,
        msg: "注册成功",
      };
    }));
});

Router.post("/login", async (ctx) => {
  const { username, password } = ctx.request.body;
  const result = await TestUser.findOne({ username, password });
  if (!result) return (ctx.body = Util.back(-401));
  return (ctx.body = {
    data: { username, token: "qwe123456" },
    code: 0,
    msg: "登录成功",
  });
});

Router.post("/temp-type", async (ctx) => {
  const { key } = ctx.request.body;
  return (ctx.body = await new TempType({ key }).save().then((res) => {
    return {
      data: key,
      code: 0,
      msg: "模板类型添加成功",
    };
  }));
});

Router.get("/temp-type", async (ctx) => {
  const result = await TempType.find();
  return (ctx.body = {
    data: result,
    code: 0,
    msg: "模板类型读取成功",
  });
});

Router.get("/temp-list", async (ctx) => {
  const { type } = ctx.query;
  const result = type ? await TempList.find({ type }) : await TempList.find();
  return (ctx.body = {
    data: result,
    code: 0,
    msg: "模板数据读取成功",
  });
});

Router.post("/temp-list", async (ctx) => {
  const { title, type, tags, remarks, _id } = ctx.request.body;
  if (_id) {
    const result = await TempList.updateOne(
      { _id },
      { title, type, tags, remarks }
    );
    if (result) {
      return (ctx.body = {
        code: 0,
        msg: "修改成功",
        data: result,
      });
    } else {
      return (ctx.body = {
        code: 401,
        msg: "修改失败",
        data: result,
      });
    }
  } else {
    return (ctx.body = await new TempList({ title, type, tags, remarks })
      .save()
      .then((res) => {
        return {
          data: res,
          code: 0,
          msg: "模板数据添加成功",
        };
      }));
  }
});

Router.get("/temp-valid-title", async (ctx) => {
  const { title } = ctx.query;
  const result = await TempList.findOne({ title });
  let msg = "";
  result ? (msg = "模板标题已存在!") : (msg = "成功");

  return (ctx.body = {
    data: result,
    code: msg === "成功" ? 0 : 401,
    msg,
  });
});

Router.post("/temp-com-data", async (ctx) => {
  const { id, content } = ctx.request.body;
  const result = await TempComData.findOne({ parentId: id });
  if (result) {
    const result = await TempComData.updateOne({ parentId: id }, { content });
    if (result) {
      return (ctx.body = {
        code: 0,
        msg: "数据修改成功",
        data: result,
      });
    } else {
      return (ctx.body = {
        code: 401,
        msg: "数据修改失败",
        data: result,
      });
    }
  } else {
    return (ctx.body = await new TempComData({ parentId: id, content })
      .save()
      .then((res) => {
        return {
          data: res,
          code: 0,
          msg: "数据保存成功",
        };
      }));
  }
});

Router.get("/temp-com-data", async (ctx) => {
  const { id } = ctx.query;
  const result = await TempComData.findOne({ parentId: id });
  let msg = "";
  result ? (msg = "读取数据成功!") : (msg = "读取数据失败");
  return (ctx.body = {
    data: result,
    code: msg === "读取数据成功!" ? 0 : 401,
    msg,
  });
});

module.exports = Router.routes();
